package com.kahluastudios.mealfinder.desktop.server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.google.gwt.user.client.Random;
import com.kahluastudios.mealfinder.desktop.shared.PromotionalCode;
import com.kahluastudios.mealfinder.desktop.shared.RecommendationVO;
import com.kahluastudios.mealfinder.desktop.shared.RestaurantInfoVO;

public class SpoofDataStore {
	
	private static final SpoofDataStore INSTANCE = new SpoofDataStore();

	private Map<Object, RecommendationVO> recommendations;
	private Map<Object, RestaurantInfoVO> restaurants;
	private Map<Object, PromotionalCode> promoCodes;
	
	private SpoofDataStore() {
		restaurants = createSpoofRestaurants();
		recommendations = createSpoofRecomendations();
		promoCodes = createSpoofPromoCodes();
	}
	
	public static SpoofDataStore getInstance() { return INSTANCE; }
	
	public Map<Object, RecommendationVO> getRecommendations() {
		return this.recommendations;
	}
	
	public Map<Object, RestaurantInfoVO> getRestaurants() {
		return this.restaurants;
	}
	
	public Map<Object, PromotionalCode> getPromoCodes() {
		return this.promoCodes;
	}
	
	private Map<Object, PromotionalCode> createSpoofPromoCodes() {
		Map<Object, PromotionalCode> result = 
								new HashMap<Object, PromotionalCode>();
		
		PromotionalCode code; 
		String bannerLine, codeValue, description;
		for (int i=0;i<50;i++) {
			
			if (i % 2==0)
				bannerLine = (i+2)+"% per person";
			else if (i % 3==0)
				bannerLine = "£"+(i+10)+" off your bill";
			else bannerLine = "1 for 2 offer";
				
			codeValue = "BGHAST"+i;
			description = "Offer valid until <b>Dec-31-2014</b>.";
			code = new PromotionalCode(bannerLine,codeValue, description);
			result.put(codeValue, code);
		}
		return result;
	}

	private Map<Object, RestaurantInfoVO> createSpoofRestaurants() {
		Map<Object, RestaurantInfoVO> result = 
				new HashMap<Object, RestaurantInfoVO>();
		
		RestaurantInfoVO restaurantInfo;
		String name, address, cuisine;
		for (int i=0;i<50;i++) {
			address = i + " ";
			if (i % 2==0) {
				name = "Franky's @ " + i ;
				address = " William Street, Cardiff, CF54 2BG";
				cuisine = "Expiremental";				
			} else if (i % 3==0) {
				name = "Billy's @ " + i ;
				address = " James Street, London, SF1 2GT";
				cuisine = "Chinese";				
			} else if (i % 5==0) {
				name = "Johnny's @ " + i ;
				address = " Foyle Street, Manchester, M01 7HA";
				cuisine = "Chinese";				
			} else {
				name = "Tonny's @ " + i ;
				address = " Foyle Street, Bristol, BF56 89G";
				cuisine = "Chinese";
			}
						
			restaurantInfo = new RestaurantInfoVO(i,name,address, cuisine);
			result.put(restaurantInfo.getId(), restaurantInfo);
		}		
		
		return result;
	}

	private Map<Object, RecommendationVO> createSpoofRecomendations() {
		Map<Object, RecommendationVO> result = 
					new HashMap<Object, RecommendationVO>();

		RecommendationVO recommendationVO;
		List<PromotionalCode> codes;
		int i=50;
		for (RestaurantInfoVO restaurantInfo : this.restaurants.values()) {
			codes = generateTestCodes(i);
			recommendationVO = new RecommendationVO(UUID.randomUUID(), restaurantInfo, codes);
			result.put(recommendationVO.getRestaurantInfo().getId(), recommendationVO);
			i--;
		}		
		return result;
	}

	private List<PromotionalCode> generateTestCodes(int i) {
		List<PromotionalCode> result = new ArrayList<PromotionalCode>();
		
		int count = i;
		if (i > 2)
			count = Random.nextInt(i);
		if (count ==0)
			count = i;
		int j=0;
		for (PromotionalCode codes : promoCodes.values()) {
			result.add(codes);
			j++;
			if (j>=count) break;
		}
		
		return result;
	}
}
